package com.sfc.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by heylear.cai on 2015/12/30.
 */
public class BeanUtils {

    public static <T> T copy(Object source, Class<T> clz) {
        return copy(source, clz, (String[]) null);
    }

    public static <T> T copy(Object source, Class<T> clz, String... ignoreProperties) {
        try {
            T dest = clz.newInstance();
            org.springframework.beans.BeanUtils.copyProperties(source, dest, ignoreProperties);
            return dest;
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> List<T> copyAll(List sources, Class<T> clz) {
        return copyAll(sources, clz, (String[]) null);
    }

    public static <T> List<T> copyAll(List sources, Class<T> clz, String... ignoreProperties) {
        List<T> arrayList = new ArrayList<>();
        for (Object source : sources) {
            arrayList.add(copy(source, clz, ignoreProperties));
        }
        return arrayList;
    }
}
